<?php
namespace app\admin\controller;

use app\common\controller\Adminbase;
use mortalauth\Mortalauth;

class Index extends Adminbase
{
    public function index()
    {
        return view('index');
    }
    public function welcome(){
        return view('welcome');
    }
    public function get_menu()
    {
        $menu = $this->get_user_menu();
        //数据处理，适配后端模板
        foreach ($menu as $key => $value) {
            $menu[$key]['title'] = $value['name'];
            unset($menu[$key]['name']);
            $menu[$key]['href'] = '/'.$value['conditions'];
            unset($menu[$key]['conditions']);
            if($value['conditions']){
                $menu[$key]['type'] = 1;
            }else{
                $menu[$key]['type'] = 0;
            }
        }
        $menu = array_recursion($menu);
        return json($menu);
    }
    public function login_out()
    {
        session(null);
        $is_login = $this->auth_check->check_login();
        if(is_array($is_login) && ($is_login['code'] == -1)){
            return json(['code'=>200,'msg'=>'已退出，请重新登录']);
        }else{
            return json(['code'=>1,'msg'=>'退出错误，请重试']);
        }
    }
    public function upload()
    {
        $file = request()->file('file');
        $type = request()->param('type');
        $exit = 'jpg,png,gif,jpeg';
        if($type == 'file'){
            $exit = 'zip,rar,doc,docx,xls,xlsx';
        }
        $info = $file->validate(['size'=>5000000,'ext'=>$exit])->move( '../public/uploads');
        if($info){
            $imgName = str_replace("\\","/",$info->getSaveName());
            $photo = '/uploads/'.$imgName;
            return json(['code'=>200,'msg'=>'上传成功','data'=>['pic_url'=>$photo],'location'=>$photo]);
        }else{
            return json(['code'=>1,'msg'=>$file->getError()]);
        }
    }
}
